﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ServiceModel;
using hostingwcfin_windownform;
using System.ServiceModel.Description;
namespace hostingwcf
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        ServiceHost host;
        private void btnstart_Click(object sender, EventArgs e)
        {
            if (btnstart.Text=="start")
            {
                Uri baseaddress = new Uri("http://localhost:1234/vanhung/service");
                Uri baseaddress1 = new Uri("net.tcp://localhost:1254/vanhung/service");
                host = new ServiceHost(typeof(hostingwcfin_windownform.Service1), baseaddress, baseaddress1);
                BasicHttpBinding binding = new BasicHttpBinding();
                NetTcpBinding netbinding = new NetTcpBinding();
                ServiceMetadataBehavior behavior = new ServiceMetadataBehavior();
                host.Description.Behaviors.Add(behavior);
                host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
                host.AddServiceEndpoint(typeof(IMetadataExchange), MetadataExchangeBindings.CreateMexTcpBinding(), "mex");
                host.AddServiceEndpoint(typeof(hostingwcfin_windownform.IService1), binding, baseaddress);
                host.AddServiceEndpoint(typeof(hostingwcfin_windownform.IService1), netbinding, baseaddress1);
                host.Open();
                if (host.State == CommunicationState.Opened)
                {
                    label1.Text = "service is running";
                    btnstart.Text = "Stop";
                }
            }
            else if(btnstart.Text=="Stop")
            {
                host.Close();
                if (host.State==CommunicationState.Closed)
                {
                    label1.Text = "service is stopped";
                    btnstart.Text = "start";
                }
            }
        }
    }
}
